//
//  TicketController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/2.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "TicketController.h"
#import "TicketCell.h"
#import "TicketModel.h"

@interface TicketController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) NSArray * titleArray;
@property (nonatomic , strong) UITableView * tableview;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , assign) NSInteger index;
@property (nonatomic , strong) NSString * currentString;
@end

@implementation TicketController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setNavi
{
    self.title = @"优惠券";
    
}

-(void)setUI
{
    self.currentString = @"using";
    SegementScrollView * view = [[SegementScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40) andtitleArray:@[@"未使用",@"已使用",@"已过期"]];
    [self.view addSubview:view];
    self.view.backgroundColor = Color(245, 245, 245);
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, kScreenWidth, kScreenHeight - 50 ) style:(UITableViewStyleGrouped)];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.backgroundColor = [UIColor clearColor];
    [self.tableview registerNib:[UINib nibWithNibName:@"TicketCell" bundle:nil] forCellReuseIdentifier:@"TicketCell"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    self.tableview.tableFooterView = [UIView new];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableview.mj_header beginRefreshing];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    view.selectedBlock = ^(NSInteger index) {
        if (index == 0) {
            self.currentString = @"using";
        }else if (index == 1){
            self.currentString = @"used";
        }else if (index == 2){
            self.currentString = @"expired";
        }
        [self.dataArray removeAllObjects];
        [self requestData];
    };
}

- (void)requestData
{
    self.index = 1;
    [self requestMoreData];
}

-(void)requestMoreData
{
    NSDictionary * dict = @{@"token":self.userToken,@"coupon_type":self.currentString};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@user-coupon/index",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            if (isSuccess) {
                if (self.index == 1) {
                    self.dataArray = [NSMutableArray array];
                }
                for (int i = 0; i < [responseObject[@"data"] count]; i ++) {
                    TicketModel * model = [[TicketModel alloc]init];
                    [model setValuesForKeysWithDictionary:responseObject[@"data"][i]];
                    [self.dataArray addObject:model];
                }
                
                if ([responseObject[@"hasmore"] integerValue] == 0) {
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }
                self.index += 1;
                [self.tableview reloadData];
                [self endRefresh];
            }
    } failure:^(NSError *error) {
        [self endRefresh];
    }];
}

- (void)endRefresh
{
    if (self.tableview.mj_header.isRefreshing) {
        [self.tableview.mj_header endRefreshing];
    }else
    {
        [self.tableview.mj_footer endRefreshing];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    [cell bindDataWithModel:self.dataArray[indexPath.section]];
    cell.contentView.backgroundColor = Color(245, 245, 245);
    if ([self.currentString isEqualToString:@"using"]) {
        cell.bgImageView.image = Image(@"补充box_券_红");
        cell.useBtn.hidden = YES;
        cell.useNowBtn.hidden = NO;
    }else if ([self.currentString isEqualToString:@"used"]){
        cell.bgImageView.image = Image(@"补充box_券_灰");
        cell.useBtn.hidden = NO;
        cell.useNowBtn.hidden = YES;
    }else if ([self.currentString isEqualToString:@"expired"]){
        cell.bgImageView.image = Image(@"补充box_券_灰");
        cell.useNowBtn.hidden = YES;
        cell.useBtn.hidden = YES;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
